//
//  AppDelegate.swift
//  微博YX
//
//  Created by apple on 15/11/22.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        创建tarBar的控制器
//        let tabBar = YXMainViewController()
        
        
//        let tabBar = YXWelcomeViewController()
//        let tabBar = YXNewFeatureViewController()
//        设置窗口的跟控制器
//        window?.rootViewController = tabBar
        
//        window?.rootViewController?.view.backgroundColor = UIColor.redColor()
        window?.rootViewController = defaultController()
        print("\(window?.rootViewController)")
        
        window?.makeKeyAndVisible()
        return true
    }
    /// MARK: -设置皮肤
    func setupAppearance(){
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }
    
    // MARK: - 其他界面切换控制器,需要通过 AppDelegate 来切换
    // 目标控制器有2个: 欢迎界面和MainViewController
    private func switchViewController(isMain : Bool){
        window?.rootViewController = isMain ? YXMainViewController() : YXWelcomeViewController()
        
    }
    
//    MARK: -类方法   方便别人调用
    class func switchViewController(isMain : Bool) {
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(isMain)
        
    }
    
    
    
   /// MARK: -判断进入哪一个控制器
    func defaultController() -> UIViewController
    {
//        没有的登录
        if !YXUserAccount.userLogin{
//            显示访客视图
            return YXMainViewController()
        }
//        已经登录
//        判断是不是新版本
        
        return isNewFeature() ? YXNewFeatureViewController() : YXWelcomeViewController()
    }
    
    
    
    /// MARK: -判断是不是新版本
    func isNewFeature() -> Bool {
//        获得当前版本号
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
//        获得上一次版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
//        保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        return currentVersion != sandboxVersion
    }
    

}

